﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-06-10 19:44:11
// 负责人：Sushee
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{
	#region 店铺信息 数据访问层
	/// <summary>
	///店铺信息 数据访问层
	/// </summary>	
	internal partial class Shop:IShop
	{		
    
            //连接字符串
            public virtual string ConnectionStringKey
            {
                get
                {
                    return ConstantUtility.ConnectionEjautobaseKey;
                }
            }
        #region GetMaxID
        
    
      
            public int GetMaxID()
            {
                int nReturn=0;
                string szSql=@"SELECT ISNULL(MAX(ShopID),0)+1
                               FROM [Shop]";             
                Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
                DbCommand dbc = db.GetSqlStringCommand(szSql);
                try
                {
                    object o = db.ExecuteScalar(dbc);
                    if (o!=null)
                    {
                        nReturn = Convert.ToInt32(o);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return nReturn;               
            }
            
      
        
        #endregion
    
		#region Basic CRUD functions
         private ShopInfo GetByReader(IDataReader dr)
         {
            ShopInfo info = new ShopInfo();
            object obj;
            obj=dr["ShopID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.ShopID = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["UserName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.UserName = obj.ToString();
            }
            obj=dr["Mobile"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.Mobile = obj.ToString();
            }
            obj=dr["WxOpenID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.WxOpenID = obj.ToString();
            }
            obj=dr["Gender"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.Gender = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["HeadImgSrcType"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.HeadImgSrcType = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["HeadImgSrc"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.HeadImgSrc = obj.ToString();
            }
            obj=dr["ShopName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.ShopName = obj.ToString();
            }
            obj=dr["ShopType"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.ShopType = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["LocationId"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.LocationId = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["Address"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.Address = obj.ToString();
            }
            obj=dr["MapLocationName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.MapLocationName = obj.ToString();
            }
            obj=dr["MapLocationAddress"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.MapLocationAddress = obj.ToString();
            }
            obj=dr["MapLatitude"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.MapLatitude = obj.ToString();
            }
            obj=dr["MapLogitude"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.MapLogitude = obj.ToString();
            }
            obj=dr["Description"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.Description = obj.ToString();
            }
            obj=dr["IsActive"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.IsActive = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["CreatedDate"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.CreatedDate = ((obj)==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime(obj);
            }
            return info;
         }
		public int Insert(ShopInfo info)
		{
        
        
            string strSql=@"INSERT INTO Shop			
			(			
			[UserName],
			[Mobile],
			[WxOpenID],
			[Gender],
			[HeadImgSrcType],
			[HeadImgSrc],
			[ShopName],
			[ShopType],
			[LocationId],
			[Address],
			[MapLocationName],
			[MapLocationAddress],
			[MapLatitude],
			[MapLogitude],
			[Description],
			[IsActive],
			[CreatedDate]
			)
			 VALUES
			(			
			@UserName,
			@Mobile,
			@WxOpenID,
			@Gender,
			@HeadImgSrcType,
			@HeadImgSrc,
			@ShopName,
			@ShopType,
			@LocationId,
			@Address,
			@MapLocationName,
			@MapLocationAddress,
			@MapLatitude,
			@MapLogitude,
			@Description,
			@IsActive,
			@CreatedDate
			)
			 SET @ShopID=@@IDENTITY
			";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddOutParameter(dbc, "@ShopID",DbType.Int32, sizeof(Int32));  
            db.AddInParameter(dbc, "@UserName",DbType.String, info.UserName);
            db.AddInParameter(dbc, "@Mobile",DbType.String, info.Mobile);
            db.AddInParameter(dbc, "@WxOpenID",DbType.String, info.WxOpenID);
            db.AddInParameter(dbc, "@Gender",DbType.Int32, info.Gender);
            db.AddInParameter(dbc, "@HeadImgSrcType",DbType.Int32, info.HeadImgSrcType);
            db.AddInParameter(dbc, "@HeadImgSrc",DbType.String, info.HeadImgSrc);
            db.AddInParameter(dbc, "@ShopName",DbType.String, info.ShopName);
            db.AddInParameter(dbc, "@ShopType",DbType.Int32, info.ShopType);
            db.AddInParameter(dbc, "@LocationId",DbType.Int32, info.LocationId);
            db.AddInParameter(dbc, "@Address",DbType.String, info.Address);
            db.AddInParameter(dbc, "@MapLocationName",DbType.String, info.MapLocationName);
            db.AddInParameter(dbc, "@MapLocationAddress",DbType.String, info.MapLocationAddress);
            db.AddInParameter(dbc, "@MapLatitude",DbType.String, info.MapLatitude);
            db.AddInParameter(dbc, "@MapLogitude",DbType.String, info.MapLogitude);
            db.AddInParameter(dbc, "@Description",DbType.String, info.Description);
            db.AddInParameter(dbc, "@IsActive",DbType.Int32, info.IsActive);
            db.AddInParameter(dbc, "@CreatedDate",DbType.DateTime, info.CreatedDate.ToNullDbDateTime());

             
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
            return (int)db.GetParameterValue(dbc,"ShopID");
         
		}
		
		
        public void Update(ShopInfo info)
		{
            StringBuilder sbSql=new StringBuilder("UPDATE Shop SET ");
            sbSql.AppendLine("[UserName] = @UserName,");
                
            sbSql.AppendLine("[Mobile] = @Mobile,");
                
            sbSql.AppendLine("[WxOpenID] = @WxOpenID,");
                
            sbSql.AppendLine("[Gender] = @Gender,");
                
            sbSql.AppendLine("[HeadImgSrcType] = @HeadImgSrcType,");
                
            sbSql.AppendLine("[HeadImgSrc] = @HeadImgSrc,");
                
            sbSql.AppendLine("[ShopName] = @ShopName,");
                
            sbSql.AppendLine("[ShopType] = @ShopType,");
                
            sbSql.AppendLine("[LocationId] = @LocationId,");
                
            sbSql.AppendLine("[Address] = @Address,");
                
            sbSql.AppendLine("[MapLocationName] = @MapLocationName,");
                
            sbSql.AppendLine("[MapLocationAddress] = @MapLocationAddress,");
                
            sbSql.AppendLine("[MapLatitude] = @MapLatitude,");
                
            sbSql.AppendLine("[MapLogitude] = @MapLogitude,");
                
            sbSql.AppendLine("[Description] = @Description,");
                
            sbSql.AppendLine("[IsActive] = @IsActive,");
                
            sbSql.AppendLine("[CreatedDate] = @CreatedDate");
                
            sbSql.Append(" where  [ShopID] = @ShopID");
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(sbSql.ToString());
            db.AddInParameter(dbc, "@ShopID",DbType.Int32, info.ShopID);
            db.AddInParameter(dbc, "@UserName",DbType.String, info.UserName);
            db.AddInParameter(dbc, "@Mobile",DbType.String, info.Mobile);
            db.AddInParameter(dbc, "@WxOpenID",DbType.String, info.WxOpenID);
            db.AddInParameter(dbc, "@Gender",DbType.Int32, info.Gender);
            db.AddInParameter(dbc, "@HeadImgSrcType",DbType.Int32, info.HeadImgSrcType);
            db.AddInParameter(dbc, "@HeadImgSrc",DbType.String, info.HeadImgSrc);
            db.AddInParameter(dbc, "@ShopName",DbType.String, info.ShopName);
            db.AddInParameter(dbc, "@ShopType",DbType.Int32, info.ShopType);
            db.AddInParameter(dbc, "@LocationId",DbType.Int32, info.LocationId);
            db.AddInParameter(dbc, "@Address",DbType.String, info.Address);
            db.AddInParameter(dbc, "@MapLocationName",DbType.String, info.MapLocationName);
            db.AddInParameter(dbc, "@MapLocationAddress",DbType.String, info.MapLocationAddress);
            db.AddInParameter(dbc, "@MapLatitude",DbType.String, info.MapLatitude);
            db.AddInParameter(dbc, "@MapLogitude",DbType.String, info.MapLogitude);
            db.AddInParameter(dbc, "@Description",DbType.String, info.Description);
            db.AddInParameter(dbc, "@IsActive",DbType.Int32, info.IsActive);
            db.AddInParameter(dbc, "@CreatedDate",DbType.DateTime, info.CreatedDate.ToNullDbDateTime());
			
           
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
		}
		
        public void Delete(int shopID)
		{
			StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM [Shop]  ");
            strSql.Append("WHERE [ShopID]=@ShopID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@ShopID",DbType.Int32, shopID);
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
          
           
		}
        
         public IList<ShopInfo> Search(out int pageCount, int pageIndex, int pageSize, string where, string orderField, bool isDesc)
        {
            IList<ShopInfo> list = new List<ShopInfo>();
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("sp_OF_Page");
            //设置参数
            db.AddOutParameter(dbc, "@pageCount", DbType.Int32, 4);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbc, "@strWhere", DbType.String, where);
            db.AddInParameter(dbc, "@fldName", DbType.String, orderField);
            db.AddInParameter(dbc, "@OrderType", DbType.Boolean, isDesc);
            db.AddInParameter(dbc, "@tblName", DbType.String, "Shop");
            db.AddInParameter(dbc, "@strGetFields", DbType.String, "*");
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    ShopInfo info = GetByReader(dr);
                    list.Add(info);
                }
            }
            pageCount = (int)db.GetParameterValue(dbc, "@PageCount");
            return list;
        }
        
      
        public ShopInfo GetByID(int shopID)
		{
            Model.ShopInfo  info = null;
		    StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [Shop] ");
            strSql.Append("WHERE [ShopID]=@ShopID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@ShopID",DbType.Int32, shopID);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
		}
		
        
		#endregion
	}
	#endregion
}